package com.wifi.user.servlet;

import com.alibaba.fastjson.JSON;
import com.wifi.annotation.ShenZhouWifiMethod;
import com.wifi.base.BaseServlet;
import com.wifi.unti.LoginUtil;
import com.wifi.user.entity.Menu;
import com.wifi.user.entity.User;
import com.wifi.user.service.MenuService;
import com.wifi.user.service.MenuServiceImpl;
import com.wifi.user.service.UserService;
import com.wifi.user.service.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/menu/*")
public class MenuManServlet extends BaseServlet {
    MenuService menuService =new MenuServiceImpl();
    LoginUtil loginUtil = new LoginUtil();



    @ShenZhouWifiMethod("/change")
    public void menuchange(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        String vlid = request.getParameter("vlid");
        System.out.println(id);
        System.out.println(vlid);
            Integer i = menuService.changeStatus(Integer.valueOf(id), Integer.valueOf(vlid));
            if (i > 0) {
                response.getWriter().write("1");
            } else {
                response.getWriter().write("2");
            }
        }
    @ShenZhouWifiMethod("/father")
    public void menufather(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Object> menufather=menuService.getMenufather();
        response.getWriter().write(JSON.toJSONString(menufather));
    }

    @ShenZhouWifiMethod("/add")
public  void addMenu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name=request.getParameter("name");
        String link=request.getParameter("link");
        String icon=request.getParameter("icon");
        String role=request.getParameter("role");
        String active=request.getParameter("active");
        System.out.println(name);
        if (name.isEmpty()){
            response.getWriter().write("3");
        }else{
            Object o=menuService.getMenu(name,Integer.valueOf(role));
            if(o==null){
                Menu menu=new Menu(null,name,Integer.valueOf(role),link,icon,Integer.valueOf(active),null);
            Long i=menuService.addMenu(menu);
            if (i>0){
                response.getWriter().write("1");
            }else{   response.getWriter().write("2");
            }
        }else{
                response.getWriter().write("4");
            }}
//        menu/add?name=zhaorui&link=1&icon=&role=4&active=1
    }

    @ShenZhouWifiMethod("/query")
    public  void queryMenu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
     Object o=   menuService.getMenuByMid(Integer.valueOf(id));
if (o==null){response.getWriter().write("1");}
else {response.getWriter().write(JSON.toJSONString(o));}
    }
    @ShenZhouWifiMethod("/edit")
    public  void editMenu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id=request.getParameter("ide");
        String name=request.getParameter("namee");
        String link=request.getParameter("linke");
        String icon=request.getParameter("icone");
        String role=request.getParameter("rolee");
        String active=request.getParameter("activee");
        System.out.println(name);
        if (name.isEmpty()){
            response.getWriter().write("3");
        }else{
              Menu menu=new Menu(Integer.valueOf(id),name,Integer.valueOf(role),link,icon,Integer.valueOf(active),null);
                Integer i=menuService.editMenu(menu);
                if (i>0){
                    response.getWriter().write("1");
                }else{   response.getWriter().write("2");
                }
            }
    }

    @ShenZhouWifiMethod("/queryAll")
    public  void queryAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Object> menuPage=menuService.getMenuPage(0,1000,"");

        response.getWriter().write(JSON.toJSONString(menuPage));

    }
}







